package com.example.news;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TabHost;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;

public class HomeActivity extends AppCompatActivity {
    private int[] newsimages = { R.mipmap.news1,  R.mipmap.news1,  R.mipmap.news1,
            R.mipmap.news1};
    private String[] newstitles = { "冒充易烊千玺诈骗小学生8万，4名00后被判刑", "民法典草案审议：常委会组成人员建议完善禁止性骚扰条款", "贵州煤矿事故多发频发 国务院安委办约谈贵州省政府", "国内真人秀节目巴厘岛违规航拍被吊销拍摄许可，领馆发布提醒" };
    private String[] newsinfos={ "人民日报 148评 2小时前", "环视全球 6165评 6小时前", "环视全球 6165评 6小时前", "环视全球 6165评 6小时前"};

    private int[] videoimages = { R.mipmap.ic_launcher,  R.mipmap.ic_launcher,  R.mipmap.ic_launcher,
            R.mipmap.news1};
    private String[] videotitles = { "秦时明月", "画江湖之不良人", "镇魂街", "哪吒之魔童转世" };
    private String[] videoinfos={ "新浪视频 148评 2小时前", "腾讯视频 6165评 6小时前", "优酷视频 6165评 6小时前", "暴风影音 6165评 6小时前"};


    ListView listviewnews=null;
    ListView listviewhot=null;
    ListView listviewvideo=null;
    Button btndetail=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        TabHost tab = (TabHost) findViewById(android.R.id.tabhost);
        //初始化TabHost容器
        tab.setup();
        //在TabHost创建标签，然后设置：标题／图标／标签页布局
        tab.addTab(tab.newTabSpec("tab1").setIndicator("新闻" , null).setContent(R.id.tab1));
        tab.addTab(tab.newTabSpec("tab2").setIndicator("视频" , null).setContent(R.id.tab2));
        tab.addTab(tab.newTabSpec("tab3").setIndicator("热荐" , null).setContent(R.id.tab3));
        tab.addTab(tab.newTabSpec("tab4").setIndicator("我的" , null).setContent(R.id.tab4));

        listviewnews=(ListView)findViewById(R.id.listviewnews);
        listviewhot=(ListView)findViewById(R.id.listviewhot);
        listviewvideo=(ListView)findViewById(R.id.listviewvideo);
        final ArrayList<HashMap<String, Object>> imagelist = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < newstitles.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("newsimage",newsimages[i]);
            map.put("newstitle", newstitles[i]);
            map.put("newsinfo",newsinfos[i]);
            imagelist.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(HomeActivity.this, imagelist, R.layout.activity_news_items, new String[] { "newsimage", "newstitle" ,"newsinfo"}, new int[] {R.id.ivnewsimage, R.id.tvnewstitle,R.id.tvnewsinfo });
        listviewnews.setAdapter(simpleAdapter);
        listviewnews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(HomeActivity.this, NewsDetailActivity.class);
               // intent.putExtra("goodsindex",String.valueOf(position));
                startActivity(intent);
            }
        });
        final ArrayList<HashMap<String, Object>> videolist = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < videotitles.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("videoimage",videoimages[i]);
            map.put("videotitle", videotitles[i]);
            map.put("videoinfo",videoinfos[i]);
            videolist.add(map);
        }
        listviewhot.setAdapter(simpleAdapter);
        SimpleAdapter simpleAdapter2 = new SimpleAdapter(HomeActivity.this, videolist, R.layout.activity_news_items, new String[] { "videoimage", "videotitle" ,"videoinfo"}, new int[] {R.id.ivnewsimage, R.id.tvnewstitle,R.id.tvnewsinfo });
        listviewnews.setAdapter(simpleAdapter);
        listviewvideo.setAdapter(simpleAdapter2);
        btndetail=(Button)findViewById(R.id.btndetail);
        btndetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(HomeActivity.this,DetailActivity.class);
                startActivity(intent);
            }
        });

    }
}
